import React from 'react';
import BaseModule from "../common/BaseModule";
import Service from "./Service";
import Lng from "../common/lng";
import {Popconfirm} from "antd";
import AddDishProcessForm from "./AddDishProcessForm";

class DishProcess extends BaseModule {
    constructor(props) {
        super(props);
        this.state.id = "";

        this._page.unionType = {
            comment: "OR"
        }
    }

    loadData() {
        this._page.items = [];
        const value = this.refs.input.input.value;
        this._page.items.push({
            queryKey: "code",
            queryValue: value,
            conditionType: "LIKE"
        }, {
            queryKey: "comment",
            queryValue: value,
            conditionType: "LIKE"
        });

        super.superLoadData(Service.listProcess);
    }

    addClick() {
        this.refs["dishProcess"].showModal();
    }

    remove(record) {
        const self = this;
        Service.removeProcess(record.id, () => {
            self.loadData();
        })
    }

    edit(record) {
        this.setState({
            id: record.id
        }, () => {
            this.refs["processEdit"].showModal();
        });
    }

    render() {
        const columns = [{
            title: Lng.get("dishProcess.code"),
            dataIndex: 'code',
            key: 'code'
        }, {
            title: Lng.get("dishProcess.comment"),
            dataIndex: 'comment',
            key: 'comment'
        }, {
            title: Lng.get("dish.name"),
            dataIndex: 'dishName',
            key: 'dishName'
        }, {
            title: Lng.get("dishProcess.processTime"),
            dataIndex: 'processTime',
            key: 'processTime',
            render: (text, record) => {
                return (record.processTime || "-") + " " + Lng.get("common." + record.processTimeUnit);
            }
        }, {
            title: Lng.get("common.operation"),
            dataIndex: 'operation',
            key: 'operation',
            render: (text, record, index) => {
                return <div className={"operation"}>
                    <a onClick={this.edit.bind(this, record)}>{Lng.get("common.edit", {name: ""})}</a>
                    <Popconfirm title={Lng.get("common.confirm")} onConfirm={this.remove.bind(this, record)}
                                okText={Lng.get("common.ok")}
                                cancelText={Lng.get("common.cancel")}>
                        <a href={"#"}>{Lng.get("common.remove")}</a>
                    </Popconfirm>
                </div>
            }
        }];
        return (
            <div id={"dish-process-content"}>
                {this.renderSearchBar(Lng.get("dishProcess.code"))}
                {this.renderTable(columns)}
                {this.renderModal("dishProcess", AddDishProcessForm, Lng.get("dishProcess.main"), Service.addProcess)}
                {this.renderEditModal("processEdit", AddDishProcessForm, Lng.get("dishProcess.main"), this.state.id, Service.updateProcess)}
            </div>

        )
    }
}

export default DishProcess;